<template>
  <div class="s_cart">
    <div class="canScroll">
      <div
        class="head"
        :style="setFixed"
        ref="Box"
        @scroll="setNavPosition"
      >
        <h3>购物车</h3>
      </div>
      <div class="empty"></div>
      <div
        class="content"
        v-if="flag"
      >
        <div class="card_list">
          <div
            class="card"
            v-for="(item,index) in this.cartGoodsList"
            :key="item.id"
          >
            <div class="shop_name">
              <label class="container">
                <input
                  type="checkbox"
                  v-model="item.s_good.search_num"
                  @click="checkedChange"
                >
                <div class="checkmark"></div>
              </label>
              <h3>OMINI定制</h3>

            </div>
            <div class="goods">
              <label class="container">
                <input
                  type="checkbox"
                  v-model="item.s_good.search_num"
                  @click="checkedChange"
                >
                <div class="checkmark"></div>
              </label>
              <div class="picture">
                <img
                  :src="item.s_good.s_goods_photos[0].path"
                  alt=""
                >
              </div>
              <div class="goodS_details">
                <h3>{{item.s_good.name}}</h3>
                <parameterCom :img="item.s_good.s_goods_photos[0].path">
                </parameterCom>
              </div>

            </div>
            <div class="price_num">
              <div class="price">
                <p>¥ {{item.s_good.price}}</p>
              </div>

              <div class="num">
                <button @click="reduce(item.id,index)">-</button>
                <span>{{item.num}}</span>
                <button @click="add(item.id,index)">+</button>
              </div>
            </div>
          </div>
        </div>
        <div class="noMore">
          <p>没有更多了哦~</p>
        </div>
      </div>
      <nut-empty
        description="无数据"
        v-else
      ></nut-empty>
    </div>
    <div class="bottom">
      <div class="left">
        <label class="container">
          <input
            type="checkbox"
            @click="selectAll"
            :checked="all"
          >
          <div class="checkmark"></div>
        </label>
        <p>全选</p>
      </div>
      <div class="right">
        <p>合计：</p>
        <span>¥{{sum}}</span>
        <button @click="settle">
          结算
        </button>

      </div>

    </div>

  </div>
</template>

<script>
// import addressListCom from '@/components/addressListCom.vue'
import parameterCom from '@/components/cartParameterCom.vue'
import { mapState } from 'vuex'
import { getCartData, upDateCart } from '@/api/product.js'
export default {
  components: {
    parameterCom,
  },
  data () {
    return {
      // 商品总价
      sum: 0,
      // 设置导航栏滚动position:fixed
      setFixed: '',
      // 复选框勾选
      all: false,
      // 购物车数据
      cartGoodsList: [],
      // 购物车有无数据
      flag: false,
      // 已勾选商品的数据
      selectedGoods_cartId: [],
    }
  },
  created () {
    // 请求购物车数据
    // this.$store.dispatch('getShopCartData')
    // if (this.cartList.length > 0) {
    //   this.flag = true
    // }
    getCartData()
      .then((res) => {
        console.log(res)
        this.cartGoodsList = res.data.result
        this.flag = true
      })
      .catch((err) => {
        console.log(err)
      })
  },
  mounted () {
    // 开启滚动条监听
    window.addEventListener('scroll', this.setNavPosition)
  },
  unmounted () {
    //离开该页面需要移除这个监听的事件
    window.removeEventListener('scroll', this.setNavPosition)
  },
  methods: {
    // 导航栏滚动超出固定
    setNavPosition () {
      let scroll_height =
        document.body.scrollTop ||
        document.documentElement.scrollTop ||
        window.pageYOffset
      if (scroll_height > 10) {
        this.setFixed = 'position:fixed'
      } else {
        this.setFixed = ''
      }
    },
    // 全选事件
    selectAll () {
      console.log(this.cartList)
      this.cartGoodsList.forEach((e) => {
        e.s_good.search_num = !this.all
      })
    },

    // 复选框事件
    checkedChange () {
      this.check = 'checked'
      console.log(this.cartGoodsList)
    },

    // 增加商品数量
    add (id, index) {
      this.cartGoodsList[index].num++
      let num = this.cartGoodsList[index].num
      upDateCart(id, num)
        .then((res) => {
          console.log(res)
        })
        .catch((err) => {
          console.log(err)
        })
    },
    // 减少商品数量
    reduce (id, index) {
      if (this.cartGoodsList[index].num > 1) {
        this.cartGoodsList[index].num--
        let num = this.cartGoodsList[index].num
        upDateCart(id, num)
          .then((res) => {
            console.log(res)
          })
          .catch((err) => {
            console.log(err)
          })
      }
    },
    // 跳转结算界面
    settle () {
      if (
        this.cartGoodsList.some((e) => {
          return e.s_good.search_num === true
        })
      ) {
        this.$router.push({
          name: 'orderSettlement',
          params: { list: this.selectedGoods_cartId },
        })
      }
    },
  },
  computed: {
    ...mapState(['goodsList', 'cartList']),
  },
  watch: {
    cartGoodsList: {
      handler () {
        // 监听购物车列表勾选变化，并赋值给总价
        let sum = 0
        this.cartGoodsList.forEach((e) => {
          if (e.s_good.search_num === true) {
            sum += e.num * e.s_good.price
          }
        })
        this.sum = sum
        // 所有商品选中，全选被勾选
        this.all = this.cartGoodsList.every((e) => {
          return e.s_good.search_num === true
        })

        // 保存勾选中的商品数据
        this.selectedGoods_cartId = []
        this.cartGoodsList
          .filter((e) => {
            return e.s_good.search_num === true
          })
          .forEach((e) => {
            this.selectedGoods_cartId.push(e.id)
          })
        console.log(this.selectedGoods_cartId)
      },
      deep: true,
    },
  },
}
</script>


<style lang="scss" scoped>
.s_cart {
  height: 90vh;
  position: relative;
  .canScroll {
    width: 100%;
    height: 100%;
    overflow: hidden;
    overflow-y: scroll;
    .head {
      width: 100vw;
      height: 80px;
      background-color: #c1ab96;
      padding: 0 20px 0 20px;
      z-index: 999;
      h3 {
        font-weight: 700;
        font-size: 20px;
        color: #ffffff;
        text-align: left;
        line-height: 80px;
      }
    }
    .empty {
      width: 100vw;
      height: 80px;
      background-color: #c1ab96;
    }
    .content {
      position: absolute;
      z-index: 998;
      top: 80px;
      padding-bottom: 50px;
      .card_list {
        width: 100vw;
        // height: 500px;
        // position: absolute;
        // top: 80px;
        display: flex;
        flex-direction: column;
        align-items: center;
        .card {
          width: 345px;
          height: 191px;
          box-sizing: border-box;
          background-color: #fff;
          margin: 0 auto 10px;
          padding: 10px;
          border-radius: 10px;
          box-shadow: rgba(0, 0, 0, 0.07) 0px 1px 2px,
            rgba(0, 0, 0, 0.07) 0px 2px 4px, rgba(0, 0, 0, 0.07) 0px 4px 8px,
            rgba(0, 0, 0, 0.07) 0px 8px 16px, rgba(0, 0, 0, 0.07) 0px 16px 32px,
            rgba(0, 0, 0, 0.07) 0px 32px 64px;
          display: flex;
          flex-direction: column;
          justify-content: space-between;
          .shop_name {
            display: flex;
            align-items: center;
            .container input {
              position: absolute;
              opacity: 0;
              cursor: pointer;
              height: 0;
              width: 0;
            }
            .container {
              display: block;
              position: relative;
              cursor: pointer;
              font-size: 15px;
              user-select: none;
            }
            .checkmark {
              position: relative;
              top: 0;
              left: 0;
              height: 20px;
              width: 20px;
              background-color: #fff;
              border: 0.5px solid #999999;
              border-radius: 25px;
              transition: 0.15s;
            }
            .container input:checked ~ .checkmark {
              background-color: #e0be8f;
              border-radius: 25px;
              transition: 0.15s;
            }
            .checkmark:after {
              content: "";
              position: absolute;
              display: none;
            }
            .container input:checked ~ .checkmark:after {
              display: block;
            }
            .container .checkmark:after {
              left: 0.45em;
              top: 0.25em;
              width: 0.25em;
              height: 0.5em;
              border: solid white;
              border-width: 0 0.15em 0.15em 0;
              transform: rotate(45deg);
            }
            h3 {
              margin-left: 10px;
              font-size: 16px;
              font-weight: 400;
            }
          }
          .goods {
            display: flex;
            justify-content: space-between;
            align-items: center;
            .container input {
              position: absolute;
              opacity: 0;
              cursor: pointer;
              height: 0;
              width: 0;
            }
            .container {
              display: block;
              position: relative;
              cursor: pointer;
              font-size: 15px;
              user-select: none;
            }
            .checkmark {
              position: relative;
              top: 0;
              left: 0;
              height: 20px;
              width: 20px;
              background-color: #fff;
              border: 0.5px solid #999999;
              border-radius: 25px;
              transition: 0.15s;
            }
            .container input:checked ~ .checkmark {
              background-color: #e0be8f;
              border-radius: 25px;
              transition: 0.15s;
            }
            .checkmark:after {
              content: "";
              position: absolute;
              display: none;
            }
            .container input:checked ~ .checkmark:after {
              display: block;
            }
            .container .checkmark:after {
              left: 0.45em;
              top: 0.25em;
              width: 0.25em;
              height: 0.5em;
              border: solid white;
              border-width: 0 0.15em 0.15em 0;
              transform: rotate(45deg);
            }
            .picture {
              width: 94px;
              height: 94px;
              margin-left: 10px;
              border-radius: 5px;
              overflow: hidden;
              img {
                width: 100%;
                height: 100%;
              }
            }
            .goodS_details {
              width: 186px;
              height: 94px;
              margin-left: 10px;
              display: flex;
              flex-direction: column;
              justify-content: space-between;
              h3 {
                font-size: 14px;
                font-weight: 400;
                line-height: 20px;
                color: #333333;
                word-break: break-all;
                /* 超出范围隐藏 */
                overflow: hidden;
                /* 文字超出用省略号 */
                text-overflow: ellipsis;
                /* 盒子模型 */
                display: -webkit-box;
                /* 显示的文本行数 */
                -webkit-line-clamp: 2;
                /* 子元素的垂直排列方式 */
                -webkit-box-orient: vertical;
              }
            }
          }
          .price_num {
            width: 186px;
            align-self: flex-end;
            display: flex;
            justify-content: space-between;
            .price {
              p {
                font-weight: 400;
                font-size: 16px;
                color: #d1082a;
                line-height: 20px;
              }
            }
            .num {
              display: flex;
              align-items: center;
              button {
                width: 20px;
                height: 20px;
                padding: 0;
                text-align: center;
                font-size: 16px;
                line-height: 15px;
                background-color: #fefbf8;
                border: 0.5px solid #eae9e5;
                cursor: pointer;
              }
              span {
                display: inline-block;
                width: 33px;
                height: 20px;
                text-align: center;
                border-top: 0.5px solid #eae9e5;
                border-bottom: 0.5px solid #eae9e5;
              }
            }
          }
        }
      }
      .noMore {
        text-align: center;
        margin-bottom: 60px;
        // position: absolute;
        // bottom: 0;
        p {
          font-weight: 700;
          font-size: 14px;
          color: #8e7e6f;
          line-height: 16px;
        }
      }
    }
  }
  .bottom {
    position: fixed;
    z-index: 999;
    bottom: 48px;
    width: 100vw;
    height: 50px;
    box-sizing: border-box;
    padding: 0 15px 0 40px;
    background-color: #fff;
    display: flex;
    align-items: center;
    justify-content: space-between;
    .left {
      display: flex;
      align-items: center;
      .container input {
        position: absolute;
        opacity: 0;
        cursor: pointer;
        height: 0;
        width: 0;
      }
      .container {
        display: block;
        position: relative;
        cursor: pointer;
        font-size: 15px;
        user-select: none;
      }
      .checkmark {
        position: relative;
        top: 0;
        left: 0;
        height: 20px;
        width: 20px;
        background-color: #fff;
        border: 0.5px solid #999999;
        border-radius: 25px;
        transition: 0.15s;
      }
      .container input:checked ~ .checkmark {
        background-color: #e0be8f;
        border-radius: 25px;
        transition: 0.15s;
      }
      .checkmark:after {
        content: "";
        position: absolute;
        display: none;
      }
      .container input:checked ~ .checkmark:after {
        display: block;
      }
      .container .checkmark:after {
        left: 0.45em;
        top: 0.25em;
        width: 0.25em;
        height: 0.5em;
        border: solid white;
        border-width: 0 0.15em 0.15em 0;
        transform: rotate(45deg);
      }
      p {
        margin-left: 15px;
        font-weight: 400;
        font-size: 16px;
        color: #515151;
      }
    }
    .right {
      display: flex;
      align-items: center;
      p {
        font-weight: 400;
        font-size: 14px;
      }
      span {
        font-weight: 400;
        font-size: 14px;
        color: #ff0000;
        margin-right: 30px;
      }
      button {
        width: 65px;
        height: 30px;
        background: inherit;
        background-color: rgba(240, 213, 184, 1);
        color: #fff;
        border: none;
        border-radius: 33px;
      }
    }
  }
}

// 购物车商品卡片参数样式
::v-deep .nut-cell__title {
  width: 186px;
  height: 50px;
  background-color: #f8f8f8;
  font-size: 14px;
  font-weight: 300;
  color: #999999;
  text-align: left;
  line-height: 50px;
}
::v-deep .nutui-iconfont,
.nut-icon,
.nut-icon-right,
.nut-cell__link {
  font-size: 14px;
  font-weight: 300;
  height: 50px;
  line-height: 50px;
  background-color: #f8f8f8;
}
::v-deep.nut-popup,
.round,
.popup-bottom {
  width: 92vw;
}
</style>